﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;

using System.Text;
using System.Windows.Forms;


namespace MapMerge
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            
            InitializeComponent();
            
        }

        //SkinEngine skinEngine = new SkinEngine();
        FileMergeFactory shapeMergeFactory;
        private bool CanStart;
        private bool CanStopMerge;
        private bool CanCancleMerge;
        private int mergeProcess;
        public delegate void UpdateMergeProcess();

        private void Form1_Load(object sender, EventArgs e)
        {
            
            
            shapeMergeFactory = new FileMergeFactory();
            shapeMergeFactory.MergingFiles += new EventHandler<EventArgs>(shapeMergeFactory_MergingFiles);
            shapeMergeFactory.MergeFinished += new EventHandler<EventArgs>(shapeMergeFactory_MergeFinished);
            CanStart = false;
            CanStopMerge = true;
            CanCancleMerge = false;
            mergeProcess = 0;
            MergeProcessLable.Text = "已完成0%";
        }


        void shapeMergeFactory_MergeFinished(object sender, EventArgs e)
        {
            MessageBox.Show("合并完成！","ShapeMerge提示信息",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
        }

        void shapeMergeFactory_MergingFiles(object sender, EventArgs e)
        {
            MergeProcessLable.Invoke(new UpdateMergeProcess(update_mergeProcessLable));
            mergeProgressBar.Invoke(new UpdateMergeProcess(update_mergeProgressBar));
        }
        public void update_mergeProcessLable()
        {
            mergeProcess = shapeMergeFactory.GetMergePercent();
            MergeProcessLable.Text = string.Format("已完成：{0}%", mergeProcess); ;
        }

        public void update_mergeProgressBar()
        {
            ++mergeProgressBar.Value;
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SelectMapFiles();
        }

        private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void saveAsbutton_Click(object sender, EventArgs e)
        {
            SaveMapFiles();
        }


        private void SaveMapFiles()
        {
            SaveFileDialog saveAsFile = new SaveFileDialog();
            saveAsFile.FilterIndex = 1;
            saveAsFile.Title = "另存为";
            saveAsFile.DefaultExt = "shp";
            saveAsFile.FileName = "MergeResult.shp";
            saveAsFile.Filter = "地图文件.shp|*.shp|所有文件*.*|*.*";
            if (DialogResult.OK == saveAsFile.ShowDialog())
            {
                saveAsComboBox.Text = saveAsFile.FileName;
            }
        }

        private void startMergeButton_Click(object sender, EventArgs e)
        {
            StartMergeShapeFiles();
        }

        private void StartMergeShapeFiles()
        {
            if (!CanStart && !CanCancleMerge && CanStopMerge)
            {
                CanStart = true;
                CanStopMerge = false;
                if (Path.GetExtension(saveAsComboBox.Text) != ".shp")
                {
                    MessageBox.Show("请选择合适的存储位置！");
                    return;
                }
                if (!Directory.Exists(Path.GetDirectoryName(saveAsComboBox.Text)))
                {
                    MessageBox.Show("请选择合适的存储位置！");
                    return;
                }
                else
                {
                    List<string> mergeFilesPaths = new List<string>();
                    foreach (ListViewItem item in fileDirectoryListView.Items)
                    {
                        mergeFilesPaths.Add(item.Text);
                    }
                    mergeProgressBar.Maximum = mergeFilesPaths.Count;
                    shapeMergeFactory.StartMerge(mergeFilesPaths, saveAsComboBox.Text);
                }

            }
        }

        private void cancleButton_Click(object sender, EventArgs e)
        {

        }

        private void SelectMapFiles()
        {
            OpenFileDialog selectMapFiles = new OpenFileDialog();
            selectMapFiles.Multiselect = true;
            selectMapFiles.FilterIndex = 0;
            selectMapFiles.Title = "选择文件";
            selectMapFiles.Filter = "地图文件.shp|*.shp";
            if (DialogResult.OK == selectMapFiles.ShowDialog())
            {
                AddMapFiles(selectMapFiles.FileNames);
            }
        }

        private void AddMapFile(string filePath)
        {
            bool IsExist = false;
            foreach (var item in fileDirectoryListView.Items)
            {
                if (item.ToString() == filePath)
                {
                    IsExist = true;
                }
            }
            if (!IsExist)
            {
                fileDirectoryListView.Items.Add(new ListViewItem(filePath));
            }
        }
        private void AddMapFiles(string[] filePaths)
        {
            foreach (var filePath in filePaths)
            {
                AddMapFile(filePath);
            }

        }
        private void AddMapFiles(List<string> filePaths)
        {
            foreach (var filePath in filePaths)
            {
                AddMapFile(filePath);
            }
        }


        /// <summary>
        /// 拖拽功能部分
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void fileDirectoryListView_DragEnter(object sender, DragEventArgs e)
        {
            Array dropObjects = (Array)e.Data.GetData(DataFormats.FileDrop);
            List<string> filePaths = new List<string>();
            for (int i = 0; i < dropObjects.Length; i++)
            {
                filePaths.Add(dropObjects.GetValue(i).ToString());
            }
            AddMapFiles(filePaths);
        }

        private void fileDirectoryListView_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

      

     
    }
}
